package com.swkl.yuan.agvtask.dataController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.swkl.yuan.agvtask.entity.AgvTask;
import com.swkl.yuan.agvtask.service.AgvTaskService;
import com.swkl.yuan.agvtask.service.SystemConfigService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Alkin
 * @Date 2024/4/16 下午11:12
 * @Version 1.0
 */
@RestController
@RequestMapping("/data")
public class dataController {

    @Resource
    AgvTaskService agvTaskService;
    @Resource
    SystemConfigService systemConfigService;


    @GetMapping("/test")
    public String test(){
        return "asdasd";
    }



    @GetMapping("/getAllAgvTaskByPage")
    public PageResultVo queryAll(AgvTask agvTask, int page, int limit){
        IPage<AgvTask> jiayangIPage = agvTaskService.getAllAgvTaskByPage(agvTask, page, limit);
        return new PageResultVo(0, jiayangIPage.getRecords(), "成功", jiayangIPage.getTotal());
    }


    @PostMapping("/addAgvTask")
    public JsonData addAgvTask(AgvTask agvTask){
        if (agvTask.insert()){
            return  JsonData.success("添加成功");
        }else {
            return JsonData.fail("添加失败请到日志内查看详情");
        }
    }


    @PostMapping("/modifySystem")
    public JsonData modify(String ip,String dbAddress,String plcType,String heartbeatOffset,String errorOffset,String requestMoveApi,String requestFinishApi){
       return JsonData.result( systemConfigService.modifySystemConfig(ip,dbAddress,plcType,heartbeatOffset,errorOffset,requestMoveApi,requestFinishApi));
    }

    @PostMapping("/modifyAGVTask")
    public JsonData modifyAGVTask(AgvTask agvTask){
        if (agvTaskService.modifyAgvTask(agvTask)){
            return  JsonData.success("修改成功");
        }else {
            return JsonData.fail("修改失败请到日志内查看详情");
        }
    }


    @PostMapping("/batchReplace")
    public JsonData batchReplace(@RequestBody Map<String, Object> request){
        // 从 Map 中提取数据
        List<String> ids = (List<String>) request.get("ids");
        int type = Integer.parseInt((String) request.get("type")); // 将字符串类型转换为整数
        String pram = (String) request.get("pram");
        if (agvTaskService.batchReplace(ids,type,pram)){
            return  JsonData.success("批量修改成功");
        }else {
            return JsonData.fail("批量修改失败请到日志内查看详情");
        }
    }

    @PostMapping("/delAGVTask")
    public JsonData delAGVTask(Long id){
        if (agvTaskService.deleteAgvTask(id)){
            return  JsonData.success("删除成功");
        }else {
            return JsonData.fail("删除失败请到日志内查看详情");
        }
    }




}
